package cn.edu.cimv.controller;

import cn.edu.cimv.constant.InfoConstant;
import cn.edu.cimv.dao.entity.Flink;
import cn.edu.cimv.pojo.Message;
import cn.edu.cimv.service.FlinkService;
import cn.edu.cimv.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * Created by 门铎 on 2016/8/2.
 */
@Controller
@RequestMapping("/cimv/flink")
public class FlinkController {
    @Autowired
    private FlinkService flinkService;
    @Autowired
    private Message message;
    @RequestMapping("list")
    public String list(Model model){
        model.addAttribute("flinkList",flinkService.queryFlink());
        return "flink/list";
    }
    @RequestMapping("new")
    public String flinkNew(){
        return "flink/new";
    }

    @RequestMapping( value = "create", method = RequestMethod.POST)
    public @ResponseBody Message create(Flink flink) {
        flink.setUpdateTime(DateUtil.toDateString(new Date()));
        flinkService.editFlink(flink);
        message.setMsg(InfoConstant.CREATE_NORMAL_SUCCESS);
        return message;
    }
    @RequestMapping(value = "delete/{id}",method = RequestMethod.GET)
    public @ResponseBody Message delete(@PathVariable("id")int id){
        flinkService.deleteFlink(id);
        message.setMsg(InfoConstant.DELETE_NORMAL_SUCCESS);
        return message;
    }
    @RequestMapping(value = "update/{id}", method = RequestMethod.GET)
    public String updatePage(@PathVariable("id")int id,Model model){
        model.addAttribute("flink",flinkService.queryFlinkById(id));
        return "flink/update";
    }
    @RequestMapping(value = "updateFlink/{id}",method = RequestMethod.POST)
    public @ResponseBody Message updateNews(@PathVariable("id")String id,
                                            @ModelAttribute Flink flink){
        //修改的内容
        flink.setId(id);
        flink.setUpdateTime(DateUtil.toDateString(new Date()));
        //更新
        flinkService.updateFlink(flink);

        message.setMsg(InfoConstant.UPDATE_NORMAL_SUCCESS);
        return message;

    }
}
